package com.baizhi.serviceImpl;

import com.baizhi.annotation.LogAnno;
import com.baizhi.dao.AdminDAO;
import com.baizhi.dto.PageDTO;
import com.baizhi.entity.Admin;
import com.baizhi.service.AdminService;
import com.baizhi.vo.CommonMessageVO;
import com.baizhi.vo.CommonVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.TransactionAnnotationParser;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class AdminServiceImpl extends ServiceImpl<AdminDAO, Admin> implements AdminService {

    private static final Logger log1 = LoggerFactory.getLogger(AdminServiceImpl.class);
    @Autowired
    private AdminDAO adminDAO;
    @Autowired
    HttpServletRequest request;

    @Override
    public Map<String, Object> login(Admin admin,String enCode) {
        Map<String, Object> map = new HashMap<>();
        String code = (String) request.getServletContext().getAttribute("code");

        //判断验证码是否正确
        if (code.equals(enCode)){
            //根据用户输入对象获取数据库
            LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Admin::getUsername,admin.getUsername());
            Admin admins = adminDAO.selectOne(queryWrapper);
            //判断用户是否为null
            if (admins!=null){
                //查看当前用户状态
                if ("1".equals(admins.getStatus())){
                    //判断密码是否正确
                    if (admins.getPassword().equals(admin.getPassword())){
                        request.getServletContext().setAttribute("admin",admins);
                        map.put("message",admins);
                        map.put("status",200);
                    }else  {
                        map.put("message","密码错误");
                        map.put("status",400);
                    }
                }else {
                    map.put("message","该账号已被冻结,详情请联系超级管理员");
                    map.put("status",400);
                }
            }else {
                map.put("message","没有该用户");
                map.put("status",400);
            }
        }else {
            map.put("message","验证码错误");
            map.put("status",400);
        }
        return map;
    }

    /**
     * 分页查所有
     *
     * @param pageDTO
     */
    @Override
    public CommonVO pageQueryAll(PageDTO pageDTO) {
        CommonVO commonVO = new CommonVO();
        Page<Admin> page = new Page<>(pageDTO.getPage(), pageDTO.getPageSize());
        Page<Admin> adminPage = adminDAO.selectPage(page, null);
        commonVO.setPage(pageDTO.getPage());
        commonVO.setTotal(adminPage.getTotal());
        commonVO.setRows(adminPage.getRecords());
        return commonVO;
    }

    /**
     * 修改
     *
     * @param admin
     */
    //@Transactional(rollbackFor = Exception.class)
    @LogAnno(name = "修改管理员状态")
    @Override
    public void update(Admin admin) throws RuntimeException{
        /*try {
            int i = adminDAO.updateById(admin);
            int a = 10/0;
            return CommonMessageVO.succeed();
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return CommonMessageVO.failed();

        }*/
        int i = adminDAO.updateById(admin);
        int a = 10/0;
    }

    /**
     * 删除
     *
     * @param admin
     */
    @Override
    public CommonMessageVO delete(Admin admin) {

        try {
            log1.info("admin :{}",admin);
            LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Admin::getId,admin.getId());
            int delete = adminDAO.delete(queryWrapper);
            return CommonMessageVO.succeed();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonMessageVO.failed();
        }
    }

    /**
     * 添加
     *
     * @param admin
     */
    @LogAnno(name = "添加管理员")
    @Override
    public CommonMessageVO add(Admin admin) {
        try {
            admin.setStatus("1");
            int i = adminDAO.insert(admin);
            //int a = 10/0;
            return CommonMessageVO.succeed();
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new RuntimeException();
            //return CommonMessageVO.failed();

        }
    }
    /*@Transactional(rollbackFor = Exception.class)
    @LogAnno(name = "添加管理员")
    @Override
    public CommonMessageVO add(Admin admin) {

        try {
            admin.setStatus("1");
            int i = adminDAO.insert(admin);
            //int a = 10/0;
            return CommonMessageVO.succeed();
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return CommonMessageVO.failed();
        }
    }*/


    /**
     * 根据id查
     *
     * @param admin
     */
    @Override
    public Admin queryById(Admin admin) {
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Admin::getId,admin.getId());
        Admin admins = adminDAO.selectOne(queryWrapper);
        log1.info("admins {}",admins);
        return admins;
    }

}
